/*
 * Created by SharpDevelop.
 * User: agsd
 * Date: 09/02/2008
 * Time: 17:15
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Serialization;
using System.Text;
using System.IO;

namespace Mogocal
{
	/// <summary>
	/// Description of Config.
	/// </summary>
	public partial class Config : Form
	{
		public Config()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
		}
		
		void ButtonSaveClick(object sender, EventArgs e)
		{
			SaveConfigSettings();
			this.Close();
		}
		
		void ConfigLoad(object sender, EventArgs e)
		{
			
			ConfigurationSettings conf = null;

            XmlSerializer serializer = new XmlSerializer(typeof(ConfigurationSettings));
            StreamReader reader = new StreamReader(util.Util.getSettingsFileName());
            conf = (ConfigurationSettings)serializer.Deserialize(reader);
            reader.Close();
			
			 textBoxGLogin.Text = conf.GoogleAccount;
			 textBoxGPassword.Text = conf.GoogleAccountPassword;
			 textBoxProxyLogin.Text = conf.ProxyUser;
			 textBoxProxyPassword.Text = conf.ProxyPassword;
			 textBoxProxyUrl.Text = conf.ProxyHost;
			 textBoxProxyPort.Text = conf.ProxyPort.ToString();
			 checkBoxEnableProxy.Checked = conf.ProxyEnabled;
			 ManagedEnableProxy();
		}
		

		
		private void SaveConfigSettings()
		{
			ConfigurationSettings conf = 
				new ConfigurationSettings(textBoxGLogin.Text,
			 			textBoxGPassword.Text,
			 			checkBoxEnableProxy.Checked,
			 			textBoxProxyUrl.Text,
			 			int.Parse(textBoxProxyPort.Text),
			 			textBoxProxyLogin.Text,
			 			textBoxProxyPassword.Text);

            util.Util.SaveConfig(conf);
		}
		

		
		void CheckBoxEnableProxyCheckedChanged(object sender, EventArgs e)
		{
			ManagedEnableProxy();
		}
		
		private void ManagedEnableProxy()
		{
			if (checkBoxEnableProxy.Checked)
			{
				textBoxProxyLogin.Enabled = true;
			 	textBoxProxyPassword.Enabled = true;
			 	textBoxProxyUrl.Enabled = true;
			 	textBoxProxyPort.Enabled = true;
			}
			else
			{
				textBoxProxyLogin.Enabled = false;
			 	textBoxProxyPassword.Enabled = false;
			 	textBoxProxyUrl.Enabled = false;
			 	textBoxProxyPort.Enabled = false;
			}
		}
	}
}
